//
//  SetView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI
import Kingfisher
import PhotosUI

struct VipView: View {
    @StateObject var viewModel = VipViewModel()
    @StateObject private var paymentManager = PaymentManager()
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
                    .frame(maxWidth: .infinity,maxHeight: .infinity)
            } else {
                VStack{
                    ScrollView {
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(alignment: .leading, spacing: 10) {
                            ForEach(Array(viewModel.items.enumerated()), id: \.element.objectId) { index, item in
                                VipItemView(bean: item,isSelected: viewModel.selectedIndex == index)
                                    .contentShape(Rectangle())
                                    .onTapGesture {
                                        viewModel.selectedIndex = index
                                    }
                            }
                            LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                                viewModel.getData(false)
                            }) {
                                //                            LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing)
                            }
                            .noMore(viewModel.noMore)
                            .preload(offset: 10)
                            
                            Text("购买须知")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 18))
                            Text("•  会员升级后在会员期内，所有功能都可以使用无限制。")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("• 遇到购买成功没有升级会员，请及时联系客服")
                                .foregroundStyle(.bgDel)
                                .font(.system(size: 14))
                            Text("•  购买后，不支持退款操作，请大家谨慎思考后再购买。")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("Pro权限")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 18))
                            //                            Text("•  支持更多表情")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            //                            Text("•  自定义布局")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            //                            Text("•  图标更换")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            //                            Text("•  目录不限制3个")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            //                            Text("•  图片支持自定义排序")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            Text("•  每周来信")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("•  来电记录")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("•  心灵日记")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            //                            Text("•  日记添加语音")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            //                            Text("•  聊天语音转换")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            //                            Text("•  图片识别")
                            //                                .foregroundStyle(.bgItemTitle)
                            //                                .font(.system(size: 14))
                            Text("•  AI回应次数赠送比例")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("•  记忆提取与记忆注入")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("•  更多功能敬请期待")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                            Text("•  以下Pro权限不包含（但会有相应赠送）")
                                .foregroundStyle(.bgDel)
                                .font(.system(size: 15))
                            Text("•  自定义图标，自定义音色，AI次数，官方云相册")
                                .foregroundStyle(.bgItemTitle)
                                .font(.system(size: 14))
                        }
                    }
                    .enableRefresh()
                    .scrollIndicators(.hidden)
                    
                    Button(action: {
                        if !userManager.isLogin() {
                            showLoginPopup()
                            return
                        }
                        if userManager.isVip() {
                            showToast("您当前已经是Pro了")
                            return
                        }
                        viewModel.getPay()
                        showLoadPopup()
                        //执行购买
                        paymentManager.purchaseProduct(withIdentifier: viewModel.getProductId()) { result in
                            // 处理回调结果
                            switch result {
                            case .purchasing:
                                showLog("正在购买...")
                            case .purchased:
                                showLog("购买成功")
                                viewModel.doBuy(productID: viewModel.getProductId())
                            case .failed(let errorMessage):
                                hideLoadPopup()
                                showLog("购买失败: \(errorMessage)")
                            case .restored(let productID):
                                showLog("恢复购买成功，产品 ID: \(productID)")
                                viewModel.doBuy(productID: productID)
                            case .deferred:
                                showLog("购买延迟处理")
                            case .invalidProductID:
                                hideLoadPopup()
                                showLog("未找到有效的产品 ID")
                                showToast("未找到有效的产品 ID")
                            case .unknown:
                                showLog("未知交易状态")
                            }
                        }
                    }) {
                        Text("解锁Pro")
                            .foregroundColor(themeManager.themeColor)
                            .font(.system(size: 16))
                            .padding(.horizontal,10)
                            .padding(.vertical,15)
                            .frame(width: 300)
                        
                    }.background(themeManager.themeColor.opacity(0.1))
                        .cornerRadius(8)
                    Button(action: {
                        if !userManager.isLogin() {
                            showLoginPopup()
                            return
                        }
                        if userManager.isVip() {
                            showToast("您当前已经是Pro了")
                            return
                        }
                        showLoadPopup()
                        //执行购买
                        paymentManager.restorePurchases { status in
                            switch status {
                            case .restored(let productID):
                                showLog("恢复购买成功，产品 ID: \(productID)")
                                viewModel.doBuy(productID: productID)
                            case .failed(let errorMessage):
                                showLog("恢复购买失败: \(errorMessage)")
                                showToast("恢复购买失败")
                                hideLoadPopup()
                            default:
                                showLog("恢复购买状态: \(status)")
                            }
                        }
                    }) {
                        Text("恢复购买")
                            .foregroundColor(.bgNormalText2)
                            .font(.system(size: 14))
                            .padding(.horizontal,10)
                            .padding(.vertical,15)
                        
                    }
                }
            }
        }.navigationTitle("解锁Pro")
            .padding().background(.bg)
            .onAppear{
                if !viewModel.isInit {
                    viewModel.getData(true)
                    viewModel.isInit = true
                }
            }
    }
}

//#Preview {
//    SetView()
//}


//定义预览提供者
struct VipView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: VipView())
    }
}
